/* 
    Project     : 48c6c450f1a4a0cc53d9585dc0fee742
    Created on  : Mar 16, 2013, 11:29:15 PM
    Author      : Truong Khuong - khuongxuantruong@gmail.com
    Description :
        Purpose of the stylesheet follows.
*/
var Categories=(function(){
    var init=false;
    var datafields=[
        { name  :   'cate_id' ,type :   'int'},
        { name  :   'cate_type' },
        { name  :   'cate_title' },
        { name  :   'cate_display_title' },
        { name  :   'cate_alias' },
        { name  :   'cate_parent_title' },
        { name  :   'cate_position',type    :   'int' },
        { name  :   'cate_link' },
        { name  :   'cate_thumb' },
        { name  :   'cate_status', type : 'bool' },
        { name  :   'cate_insert' },
        { name  :   'cate_update' },
        { name  :   'cate_value' },
        { name  :   'cate_target' },
        { name  :   'cate_desc' }
    ],
    columns=[
        { 
            text: 'Id'        , dataField: 'cate_id'          ,width:60,filterable:false ,sortable: false,
            cellsrenderer : function (row, column, value) {
                var str="<span style='margin: 4px; float: left;'>";
                if(value && value>0){
                    try{
                        //value = $.parseJSON(value);
                        str+="\
                            <div onclick=\"Categories.EditItem('"+value+"');\" \
                            class='w-16 h-16 f-l m-r-4 c-s-p hover-50 edit-icon' title='Edit'></div>\
                        ";
                        str+="\
                            <div onclick=\"Categories.DeleteItem('"+value+"');\" \
                            class='w-16 h-16 f-l m-r-4 c-s-p hover-50 del-icon' title='Delete'></div>\
                        ";
                    }catch(e){ErrorMsg(e.message);}
                }
                str+="</span>";
                return str;
            }
        },
        { 
            text: 'Thumb'     , dataField: 'cate_thumb'       ,width:60,filterable:false ,sortable: false,
            cellsrenderer : function (row, datafield, value) {
                if(value==undefined || value=='')
                    return '<img class="m-0-a d-p-b p-t-2" height="22" src="'+base_url+'libraries/images/22/picture.png"/>';
                else
                    return '<img class="m-0-a d-p-b p-t-2" height="22" src="/data/thumbs/' + value + '"/>';
            }
        },
        { text: 'Title'     , dataField: 'cate_display_title'       ,filterable:false },
        { text: 'Parent'    , dataField: 'cate_parent_title'     ,width:100,filterable:false },
        { text: 'Position'  , dataField: 'cate_position'    ,width:80,filterable:false, cellsalign:'right',columntype:'numberinput',filtertype:'number'},
        { text: 'Type'      , dataField: 'cate_type'        ,width:100,filterable:false ,sortable: false},
        { text: 'Status'    , dataField: 'cate_status'      ,width:80,filterable:false ,columntype:'checkbox',threestatecheckbox:false,filtertype:'bool'},
        { text: 'Insert'    , dataField: 'cate_insert'      ,width:120,filterable:false ,cellsformat:'yyyy-MM-dd HH:mm:ss'},
    ],
    source,dataAdapter,
    grid_element='#jqxGrid_Backend_Categories';
    return {
        OnInit:function(){
            try{
                NoticeMsg('beforeprocessing: function(data){ source.totalrecords = data.total_rows; }');
                WarningMsg("<pre>filter      : function(){ $(\"#jqxReportGrid\").jqxGrid('updatebounddata', 'filter'); },\
            sort        : function(){ $(\"#jqxReportGrid\").jqxGrid('updatebounddata'); },\
            root        : 'rows',\
            beforeprocessing: function(data){ source.totalrecords = data.total_rows; }</pre>")
                if(init) return;
                init=true;
                //grid_element=$('#jqxGrid_Backend_Categories');
                source ={
                    datatype: "json",type : "POST",
                    datafields: datafields,
                    id: 'cate_id',
                    url: base_url+'backend/category/binding/news.html',
                    filter      : function(){ $(grid_element).jqxGrid('updatebounddata', 'filter'); },
                    sort        : function(){ $(grid_element).jqxGrid('updatebounddata'); },
                    root        : 'rows',
                    beforeprocessing: function(data){ source.totalrecords = data.total_rows; }
                };
                dataAdapter = new $.jqx.dataAdapter(source, {
                    loadError: function(xhr, status, error){
                        ErrorMsg("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+error+"<br/>");
                    }
                });
                $(grid_element).jqxGrid({
                    rendergridrows: function(obj){
                        return obj.data;    
                    },ready: function () {
                        Categories.Filter('cate_type','news');
                    },
                    width: '100%',//height:'100%',
                    source: dataAdapter,
                    theme: theme,sortable: true,
                    //filterable: true,
                    //autoshowfiltericon: true,
                    //showfilterrow: true,
                    showemptyrow: true,
                    virtualmode:false,
                    columns: columns
                });
            }catch(e){
                ErrorMsg(e.message);
            }
        },
        AddItem:function(){
            this.EditItem(0);
        },
        EditItem:function(Id){
            $('#category_edit_panel_popup').load(base_url+"backend/category/loadEditPanel/news",{Id:Id},function(){
                $('#category_edit_panel_tabs').jqxTabs({
                        theme: 'metro', height: 'auto',  scrollable: true, 
                        selectionTracker: true, animationType: 'fade'
                });
                bckdialog({
                    message:$('#category_edit_panel_popup'),
                    title:'Category edit panel'
                }).open();
                $("#category_form_edit_panel").validationEngine();
            });
            //$("#category_edit_panel_popup")
        },
        Save:function(){
            
        },
        Cancel:function(){
            
        },
        DeleteItem:function(id){
            accessdenied();
        },
        RestoreItem:function(){
            accessdenied();
        },
        Permission:function(id){
            
        },
        PrivateItem:function(){
            accessdenied();
        },
        Setting:function(){
            bckdialog({
                message:$('#window-sOpt'),
                icon:"<img class='p-a t-7 l-8' src='"+base_url+"/libraries/images/16/option.gif'/>",
                title:'Setting'
            }).open();
        },
        Refresh:function(){
            //$("#jqxGrid").jqxGrid('updatebounddata');
            Msg('✔ Dép bông nhung mềm mịn - 68.000 | Bộ màu vẽ 68 món - 85.000 | Máy uốn tóc new - 135.000');
        },
        Filter:function(datafield,filtervalue){
            var filtertype = 'stringfilter';
            var filtergroup = new $.jqx.filter();
            var filter_or_operator = 1;
            var filtercondition = 'equal';
            var filter = filtergroup.createfilter(filtertype, filtervalue, filtercondition);
            filtergroup.addfilter(filter_or_operator, filter);
            
            //$(grid_element).jqxGrid('clearfilters');
            // add the filters.
            $(grid_element).jqxGrid('addfilter', datafield, filtergroup);
            // apply the filters.
            $(grid_element).jqxGrid('applyfilters');
        }
    };
}());
$(document).ready(function () {
    //Btree().Init();
});